package com.google.code.resistorboy;

/**
 * Calculate resistance.
 * @author leandro
 */
public class ResistorCalculator {

    //translate 4 band index to values
    public static Resistance calculate(int a, int b, int c, int d) {
        Resistance r = new Resistance();
        float significantDigits = ((a + 1) * 10) + b;

        switch (c) {
            case 0:
                r.setExp("");
                break;
            case 1:
                significantDigits *= 10;
                r.setExp("");
                break;
            case 2:
                significantDigits *= 100;
                r.setExp("");

                if (significantDigits >= 1000) {
                    significantDigits /= 1000;
                    r.setExp("k");
                }

                break;
            case 3:
                significantDigits *= 1;
                r.setExp("k");
                break;
            case 4:
                significantDigits *= 10;
                r.setExp("k");
                break;
            case 5:
                significantDigits *= 100;
                r.setExp("k");

                if (significantDigits >= 1000) {
                    significantDigits /= 1000;
                    r.setExp("M");
                }
                break;
            case 6:
                significantDigits *= 1;
                r.setExp("M");
                break;
            case 7:
                significantDigits *= 10;
                r.setExp("M");
                break;
            case 8:
                significantDigits *= 0.1;
                r.setExp("");
                break;
            case 9:
                significantDigits *= 0.01;
                r.setExp("");
                break;
        }
        
        r.setValue(significantDigits);
        r.setTolerance(getTolerance(d));
        r.setTemperature(Resistance.UNKNOWN);
        return r;
    }

    public static Resistance calculate(int a, int b, int c, int d,
            int e) {
        return calculate(a, b, c, d, e, Resistance.UNKNOWN);
    }

    public static Resistance calculate(int a, int b, int c, int d,
            int e, int f) {
        Resistance r = new Resistance();
        float significantDigits = ((a + 1) * 100) + (b * 10) + c;

        switch (d) {
            case 0:
                r.setExp("");
                break;
            case 1:
                significantDigits *= 10;
                r.setExp("");
                break;
            case 2:
                significantDigits *= 100;
                r.setExp("");

                if (significantDigits >= 1000) {
                    significantDigits /= 1000;
                    r.setExp("k");
                }

                break;
            case 3:
                significantDigits *= 1;
                r.setExp("k");
                break;
            case 4:
                significantDigits *= 10;
                r.setExp("k");
                break;
            case 5:
                significantDigits *= 100;
                r.setExp("k");

                if (significantDigits >= 1000) {
                    significantDigits /= 1000;
                    r.setExp("M");
                }

                break;
            case 6:
                significantDigits *= 1;
                r.setExp("M");
                break;
            case 7:
                significantDigits *= 10;
                r.setExp("M");
                break;
            case 8:
                significantDigits *= 0.1;
                r.setExp("");
                break;
            case 9:
                significantDigits *= 0.01;
                r.setExp("");
                break;
        }

        r.setValue(significantDigits);
        r.setTolerance(getTolerance(e));

        //temperature tolerance
        if (f != Resistance.UNKNOWN) {
            r.setTemperature(getTemperature(f));
        }

        return r;
    }

    private static int getTolerance(int t) {
        switch (t) {
            case 0: return 1;  // brown
            case 1: return 2;  // red
            case 2: return 5;  // gold
            case 3: return 10; // silver
        }
        return 0;
    }

    //in PPM/C
    private static int getTemperature(int t) {
        switch (t) {
            case 0: return 100; // brown
            case 1: return 50;  // red
            case 2: return 15;  // orange
            case 3: return 25;  // yellow
        }
        return 0;
    }

}
